<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="关键字" prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="用户名/标题/手机号" clearable style="width: 200px"
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <div class="flex-x-between">
          <div>
            <el-button v-hasPerm="['sys:user:delete']" type="danger" :disabled="removeIds.length === 0"
              @click="handleDelete()">
              <i-ep-delete />
              删除
            </el-button>
          </div>
        </div>
      </template>

      <el-table v-loading="loading" :data="pageData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column key="id" label="编号" align="center" prop="id" width="80" />
        <el-table-column label="用户名" align="center" prop="name" />
        <el-table-column label="手机号码" align="center" prop="tel" width="120" />
        <el-table-column label="标题" align="center" prop="title" width="120" />
        <el-table-column label="管理员备注" align="center" prop="admin_msg" width="320" />
        <el-table-column label="描述" align="center" prop="othor" width="180" />
        <el-table-column label="操作" fixed="right" width="150">
          <template #default="scope">
            <el-button v-hasPerm="['send/remarks']" type="primary" link size="small" @click="handleRemarks(scope.row)">
              <i-ep-edit />
              备注
            </el-button>
            <el-button v-hasPerm="['send/del']" type="danger" link size="small" @click="handleDelete(scope.row.id)">
              <i-ep-delete />
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "User",
  inheritAttrs: false,
});

import SendAPI, { UserPageQuery, UserPageVO } from "@/api/investment";
import { formatTimestamp } from '@/utils/index'

const queryFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref([]);
const total = ref(0);
const pageData = ref<UserPageVO[]>();
/** 用户查询参数  */
const queryParams = reactive<UserPageQuery>({
  pageNum: 1,
  pageSize: 10,
  // startDate: "",
  // endDate: ""
});


const handleRemarks = (row: { [key: string]: any }) => {
  ElMessageBox.prompt(
    "请输入用户「" + row.name + "」的备注",
    "备注",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      inputType: 'textarea',
    },
  ).then(
    ({ value }) => {
      SendAPI.sendRemarks({ id: row.id, remarks: value }).then(() => {
        ElMessage.success("操作成功");
        handleResetQuery();
      });
    },
    () => {
      ElMessage.info("已取消");
    }
  );
}
/** 查询 */
const dateCollect = ref('');
function handleQuery() {
  loading.value = true;
  if (dateCollect.value.length > 0) {
    queryParams.startDate = dateCollect.value[0];
    queryParams.endDate = dateCollect.value[1];
  }
  SendAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}


/** 删除用户 */
function handleDelete(id?: number) {
  const userIds = id ? [id] : removeIds.value;

  if (!userIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    function () {
      loading.value = true;
      SendAPI.deleteByIds({ ids: userIds })
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    function () {
      ElMessage.info("已取消删除");
    }
  );
}
onMounted(() => {
  handleQuery();
});
</script>
